package cn.seimes.zjcc.service.impl;

import cn.seimes.exception.enums.GlobalException;
import cn.seimes.exception.enums.GlobalExceptionEnum;
import cn.seimes.result.JsonResult;
import cn.seimes.zjcc.UserDto;
import cn.seimes.zjcc.domain.MovieTicket;
import cn.seimes.zjcc.dto.AssertUtil;
import cn.seimes.zjcc.dto.MovTicDto;
import cn.seimes.zjcc.mapper.MovieTicketMapper;
import cn.seimes.zjcc.service.IMovieTicketService;
import com.alibaba.druid.sql.ast.expr.SQLAllExpr;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qiang
 * @since 2023-01-02
 */
@Service
public class MovieTicketServiceImpl extends ServiceImpl<MovieTicketMapper, MovieTicket> implements IMovieTicketService {

    @Autowired
    private MovieTicketMapper mapper;

    @Override
    public JsonResult buyTick(MovTicDto dto) {
        // 参数jsr303校验
        MovieTicket movieTicket = selectById(dto.getTicketId());
        Long stock = movieTicket.getStock();
        if(stock==0L||stock==null){
            return JsonResult.error("没有票啊");
        }

        Long yu = movieTicket.getStock() - dto.getNumber();
        movieTicket.setStock(yu);
        updateById(movieTicket);


       Long yuan= mapper.selectbyMUser(1L);
       Long k= yuan+dto.getNumber();
        mapper.updateUserM(k);

        BigDecimal price = movieTicket.getPrice();
        BigDecimal multiply = price.multiply(new BigDecimal(dto.getNumber()));


        Double v = multiply.doubleValue();
        Double v2 = dto.getBleance().doubleValue();
        Double v1 = v2 - v;
        AssertUtil.isTrue(v1>=0,GlobalExceptionEnum.ERROR);
        return JsonResult.success(v1);
    }

    @Override
    public JsonResult selectByIda(Long id) {
        if(id==null){
            return JsonResult.error("id为空啊");
        }
        Long aLong = mapper.selectbyMUserOfMid(1L);

        long aLong2 = mapper.selectbyMUser(1L);
        if(id!=aLong){
            return JsonResult.error("你没有这个票");
        }
        if(aLong2<=0){
            return JsonResult.error("你的票已经被使用");
        }

        MovieTicket movieTicket = super.selectById(1L);

        Date startTime =new Date();
        Date endTime = movieTicket.getEndTime();
        if(endTime.getTime()<=startTime.getTime()){
            return JsonResult.error("电影票过期");
        }
        Long startCount = mapper.selectbyMUser(1L);
        mapper.updateUserM(startCount-1);
        return JsonResult.success();
    }

    @Override
    public JsonResult fuli(Long id) {
        if(id==null){
            return JsonResult.error("id为空啊");
        }
//        Long[] longs = mapper.selectByMis();
        Long[] longs = selectList(null).stream().map(movieTicket -> movieTicket.getId()).toArray(Long[]::new);
        //List<Long> collect = selectList(null).stream().filter(movieTicket -> movieTicket.getId() != null).collect(Collectors.toList());
        Integer length = longs.length;


        Random random = new Random();//创建随机对象
        int arrIdx = random.nextInt(length-1);//随机数组索引，nextInt(len-1)表示随机整数[0,(len-1)]之间的值

        Long num = longs[arrIdx];//获取数组值

        MovieTicket movieTicket = selectById(num);
        Long stock = movieTicket.getStock();
        movieTicket.setStock((stock-1));

        updateById(movieTicket);
        UserDto userDto = new UserDto(id, num, 1L);
        mapper.insertUM(userDto);
        return JsonResult.success();
    }
}
